﻿package mortal.game.scene3D.model.player
{
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import flash.utils.*;
    import frEngine.core.mesh.*;
    import frEngine.effectEditTool.manager.*;
    import frEngine.loaders.resource.info.*;
    import frEngine.manager.*;
    import mortal.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;

    public class EffectPlayer extends Obj3dContainer implements IGame2D, IHang
    {
        public var loadPriority:int = 3;
        protected var _curUrl:String;
        protected var _y2d:Number = 0;
        protected var _x2d:Number = 0;
        protected var _hangBoneName:String;
        protected var _direction:Number = 0;
        public var loaded:int = 0;
        public var disposeTime:Number = 0;
        protected var _hangList:Dictionary;
        protected var _timeoutId:int = -1;
        private var _data:Object;
        public var data:Object;
        static var _countId:uint = 0;
        private static const loadedMap:Dictionary = new Dictionary(false);

        public function EffectPlayer(param1:String, param2:RenderList, param3:int = 3)
        {
            this._hangList = new Dictionary(false);
            super(param1, _countId++);
            this.loadPriority = param3;
            renderList = param2;
            if (param1)
            {
                this.load(param1);
            }
            return;
        }// end function

        public function hang(param1:IHang, param2:String) : void
        {
            if (this.loaded == 2)
            {
                this.hangImp(param1, param2);
            }
            else
            {
                this._hangList[param1] = param2;
            }
            return;
        }// end function

        protected function hangImp(param1:IHang, param2:String) : void
        {
            var _loc_3:* = this.getObject3dByID(param2) as Md5Mesh;
            if (_loc_3)
            {
                _loc_3.targetMd5Controler.attachObjectToBone(param1.hangBoneName, param1.hangBody);
            }
            return;
        }// end function

        public function unHang(param1:IHang, param2:String) : void
        {
            var _loc_3:Md5Mesh = null;
            if (this.loaded == 2)
            {
                _loc_3 = this.getObject3dByID(param2) as Md5Mesh;
                if (_loc_3)
                {
                    _loc_3.targetMd5Controler.removeHang(param1.hangBody);
                }
            }
            else
            {
                delete this._hangList[param1];
            }
            return;
        }// end function

        public function set direction(param1:Number) : void
        {
            this._direction = param1;
            this.rotationY = Scene3DUtil.change2Dto3DRotation(this._direction);
            return;
        }// end function

        public function get direction() : Number
        {
            return this._direction;
        }// end function

        public function get hangBody() : Pivot3D
        {
            return this;
        }// end function

        public function get hangBoneName() : String
        {
            return this._hangBoneName;
        }// end function

        public function set hangBoneName(param1:String) : void
        {
            this._hangBoneName = param1;
            return;
        }// end function

        public function set x2d(param1:Number) : void
        {
            this._x2d = param1;
            x = Scene3DUtil.change2Dto3DX(param1);
            return;
        }// end function

        public function get x2d() : Number
        {
            return this._x2d;
        }// end function

        public function set y2d(param1:Number) : void
        {
            this._y2d = param1;
            z = Scene3DUtil.change2Dto3DY(param1);
            return;
        }// end function

        public function get y2d() : Number
        {
            return this._y2d;
        }// end function

        override public function set isDisposed(param1:Boolean) : void
        {
            super.isDisposed = param1;
            if (this._curUrl != null && param1)
            {
            }
            return;
        }// end function

        public function load(param1:String, param2:Object = null) : void
        {
            var _loc_6:Mesh3D = null;
            if (isDisposed || this.isInPool)
            {
                throw new Error("出错了，EffectPlayer 已被销毁！");
            }
            if (this._curUrl != null && this.loaded > 0)
            {
                throw new Error("不能重复加载！");
            }
            var _loc_7:* = param1;
            this._curUrl = param1;
            this.effectUrl = _loc_7;
            var _loc_3:* = GameScene3dConfig.instance.getEffectUrlById(param1);
            var _loc_4:* = ResConfig.instance.getEffectTemplateByName(_loc_3);
            if (_loc_4)
            {
                setTempleByName(_loc_4);
            }
            if (!Global3D.loadResource)
            {
                return;
            }
            var _loc_5:* = this.getAllObject3d();
            for each (_loc_6 in _loc_5)
            {
                
                this.disposeChild(_loc_6);
            }
            if (param2 != null)
            {
                this.loadedHander(null, param2);
            }
            else
            {
                Resource3dManager.instance.load(_loc_3, this.loadedHander, this.loadPriority);
            }
            return;
        }// end function

        public function gotoAndPlay(param1:uint) : void
        {
            this.__timeControler.gotoFrame(param1, param1);
            this.play(this.animationMode);
            return;
        }// end function

        public function gotoAndStop(param1:uint) : void
        {
            this.__timeControler.gotoFrame(param1, param1);
            this.stop();
            return;
        }// end function

        private function impParser() : void
        {
            var _loc_1:Object = null;
            this.loaded = 2;
            this.setData(this._data, false, false, renderList);
            this.__timeControler.duringFrame = this.maxKeyFrame;
            for (_loc_1 in this._hangList)
            {
                
                this.hangImp(IHang(_loc_1), this._hangList[_loc_1]);
            }
            this._hangList = new Dictionary(false);
            return;
        }// end function

        protected function loadedHander(param1:EffectInfo, param2:Object = null) : void
        {
            if (this.isDisposed)
            {
                return;
            }
            if (param1.name != this._curUrl + ".effect")
            {
                throw new Error("加载不匹配！");
            }
            if (param1)
            {
                param2 = param1.obj;
            }
            this._data = param2;
            loadedMap[this.id] = this;
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            if (isInPool || this.isDisposed)
            {
                return;
            }
            if (this.loaded == 2)
            {
                super.update(param1);
            }
            else
            {
                checkShake();
            }
            return;
        }// end function

        public function set angle(param1:Number) : void
        {
            this.rotationY = param1;
            return;
        }// end function

        override public function reset() : void
        {
            var _loc_4:Mesh3D = null;
            var _loc_5:int = 0;
            this.y2d = 0;
            this.x2d = _loc_5;
            this.angle = 0;
            this._direction = 0;
            this._hangBoneName = null;
            this._hangList = new Dictionary(false);
            this.offsetTransform = null;
            this.setPosition(0, 0, 0);
            this.setScale(1, 1, 1);
            this.setRotation(0, 0, 0);
            var _loc_1:* = getAllObject3d();
            var _loc_2:* = _loc_1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = _loc_1[_loc_3];
                if (_loc_4 is ParticleMesh)
                {
                    ParticleMesh(_loc_4).particleControler.reset();
                }
                _loc_3++;
            }
            this.disposeTime = 0;
            super.reset();
            return;
        }// end function

        override protected function playEndHander() : void
        {
            super.playEndHander();
            if (this.animationMode != PlayMode.ANIMATION_LOOP_MODE)
            {
                this.dispose(true);
            }
            return;
        }// end function

        override public function disposeChild(param1:Pivot3D) : void
        {
            var _loc_6:Mesh3D = null;
            var _loc_2:* = Global3D.scene;
            var _loc_3:* = new Array();
            getMeshesInObject(_loc_3, param1);
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_3[_loc_5];
                _loc_2.removeSelectObject(_loc_6, false);
                _loc_6.dispose(false);
                this.unRgisterObjectByObj3d(_loc_6);
                _loc_5++;
            }
            return;
        }// end function

        override public function play(param1:int = 0) : void
        {
            if (isInPool || this.isDisposed)
            {
                return;
            }
            super.play(param1);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            var _loc_2:String = null;
            if (this.isDisposed || param1 && (isInPool || this._timeoutId > 0))
            {
                return;
            }
            this.animationMode = PlayMode.ANIMATION_STOP_MODE;
            this.game3DControler = null;
            if (this._timeoutId > 0)
            {
                clearTimeout(this._timeoutId);
                this._timeoutId = -1;
            }
            if (!param1)
            {
                delete loadedMap[this.id];
                this._data = null;
                this.renderList = null;
                if (this.isInPool)
                {
                    EffectPlayerPool.instance.removeObjectFromPool(this);
                }
                if (this._curUrl != null)
                {
                    _loc_2 = GameScene3dConfig.instance.getEffectUrlById(this._curUrl);
                    Resource3dManager.instance.unLoad(_loc_2, this.loadedHander);
                    this._curUrl = null;
                }
                this.loaded = 0;
                this.loadPriority = 3;
                super.dispose(true);
                this.disposeTime = 0;
                this.data = null;
            }
            else if (this.parent && isPlaying && !impRemove)
            {
                onFightHitedHander(0, true);
                this._timeoutId = setTimeout(this.impRemoveFromeScene, 500);
            }
            else
            {
                this.impRemoveFromeScene();
            }
            return;
        }// end function

        private function impRemoveFromeScene() : void
        {
            if (this._timeoutId > 0)
            {
                clearTimeout(this._timeoutId);
                this._timeoutId = -1;
                onFightHitedHander(1, false);
            }
            resetTimerContorler(__timeControler);
            this.data = null;
            this.parent = null;
            this.renderList = null;
            this.stop();
            this.rotationY = 0;
            EffectPlayerPool.instance.dispose(this);
            this.useFightedPoint = false;
            this.game3DControler = null;
            this.temple.unHangAll(this);
            return;
        }// end function

        public static function checkToParser() : void
        {
            var _loc_1:EffectPlayer = null;
            for each (_loc_1 in loadedMap)
            {
                
                if (_loc_1._data && _loc_1.renderList)
                {
                    _loc_1.impParser();
                    _loc_1._data = null;
                    delete loadedMap[_loc_1.id];
                    break;
                }
            }
            return;
        }// end function

    }
}
